package dataIO;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

/**
 *
 * @author Administrator
 */
public class mySQLIO {
    private static mySQLConnection conn;
    private static ResultSet rs;
    
    public static HashMap<String, ArrayList<String>> getJewelryTypes() {
        conn = new mySQLConnection();
        rs = conn.executeQuery("SELECT type, material FROM jewelry", "getJewelryTypes");
        HashMap<String, ArrayList<String>> list = new HashMap<String, ArrayList<String>>();
        try {
            rs.beforeFirst();
            while(rs.next()) {
                String type = rs.getString("type");
                String material = rs.getString("material");
                ArrayList<String> ls = new ArrayList<String>();
                if(list.containsKey(type)) {
                    ls = list.get(type);
                    ls.add(material);
                    list.put(type, ls);
                } else {
                    ls.add(material);
                    list.put(type, ls);
                }
            }
            conn.finalize();
        } catch (Exception e) {
            e.printStackTrace();
        }
        conn.finalize();
        return list;
    }
    
    public static ArrayList<String> getPictures(String type, String material) {
        ArrayList<String> list = new ArrayList<String>();
        conn = new mySQLConnection();
        String query = "SELECT path FROM picture WHERE idpicture IN"
                + "(SELECT idpicture FROM jewelry_picture WHERE idjewelry IN"
                + "(SELECT idjewelry FROM jewelry WHERE material='" + material +
                "' and type='" + type + "'))";
        rs = conn.executeQuery(query, "getPictures");
        try {
            rs.beforeFirst();
            while(rs.next()) {
                String path = rs.getString("path");
                list.add(path);
            }
            conn.finalize();
        } catch (Exception e) {
            e.printStackTrace();
            conn.finalize();
        }
        return list;
    }
    
    public static void addPicture(String path) {
        conn = new mySQLConnection();
        String query = "INSERT value(path) INTO picture(path)";
        conn.executeCommand(query, "addPicture");
        try {
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
